package com.ruoyi.web.controller.demo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.DemoGoods;
import com.ruoyi.demo.service.base.DemoGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品Controller
 * 
 * @author zhangshu
 * @date 2021-02-02
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/goods" )
public class DemoGoodsController extends BaseController {

    private final DemoGoodsService iDemoGoodsService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(DemoGoods demoGoods) {
        startPage();
        List<DemoGoods> list = iDemoGoodsService.queryList(demoGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:export')" )
    @Log(title = "商品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(DemoGoods demoGoods) {
        List<DemoGoods> list = iDemoGoodsService.queryList(demoGoods);
        ExcelUtil<DemoGoods> util = new ExcelUtil<DemoGoods>(DemoGoods.class);
        return util.exportExcel(list, "goods" );
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:query')" )
    @GetMapping(value = "/{goodsId}" )
    public AjaxResult getInfo(@PathVariable("goodsId" ) Long goodsId) {
        return AjaxResult.success(iDemoGoodsService.getById(goodsId));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:add')" )
    @Log(title = "商品" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoGoods demoGoods) {
        return toAjax(iDemoGoodsService.save(demoGoods) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:edit')" )
    @Log(title = "商品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoGoods demoGoods) {
        return toAjax(iDemoGoodsService.updateById(demoGoods) ? 1 : 0);
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('demo:goods:remove')" )
    @Log(title = "商品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}" )
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(iDemoGoodsService.removeByIds(Arrays.asList(goodsIds)) ? 1 : 0);
    }
}
